﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Orchard.Model;

namespace Orchard.View
{
    partial class OrchardView : Form
    {
        OrchardModel model;
        public float HomeDateDefaultFontSize;


        public OrchardView(OrchardModel viewModel)
        {
            //view keeps instance of the model
            model = viewModel;

            //create window components
            InitializeComponent();

            HomeDateDefaultFontSize = HomeDate.Font.Size;

            //initialize separate component panes
            ProductivityControl.specializeControl(TreeTypeEnum.ProductivityTree);
            LearningControl.specializeControl(TreeTypeEnum.LearningTree);
            ExerciseControl.specializeControl(TreeTypeEnum.ExerciseTree);

            //display the home panel
            displayHomePanel();
        }

        public TreeTypeEnum currentlyDisplayed()
        {
            //productivity panel is being displayed
            if (ProductivityPanel.Visible)
            {
                return TreeTypeEnum.ProductivityTree;
            }

            //learning panel is being displayed
            else if (LearningPanel.Visible)
            {
                return TreeTypeEnum.LearningTree;
            }

            //exercise panel is being displayed
            else
            {
                return TreeTypeEnum.ExerciseTree;
            }
        }

        public void displayHomePanel()
        {
            //set date
            updateDate();

            //set all panels invisible
            ProductivityPanel.Visible = false;
            LearningPanel.Visible = false;
            ExercisePanel.Visible = false;

            //display onnly the home panel
            HomePanel.Visible = true;

            //check if data needs to be updated
            setUpdateNotifications();
        }

        public void displayProductivityPanel()
        {
            //set all panels invisible
            HomePanel.Visible = false;
            LearningPanel.Visible = false;
            ExercisePanel.Visible = false;

            //hide necessary controls if trees are up-to-date
            if (model.productivityUpToDate())
            {
                ProductivityControl.InfoBox.Enabled = false;
                ProductivityControl.SubmitButton.Enabled = false;
            }

            else
            {
                ProductivityControl.InfoBox.Enabled = true;
                ProductivityControl.SubmitButton.Enabled = true;
            }

            //set date information
            ProductivityControl.DayLabel.Text = model.ProductivityDate.ToString("dddd");
            ProductivityControl.DateLabel.Text = model.ProductivityDate.ToString("d");
            
            //display only the productivity panel
            ProductivityPanel.Visible = true;
        }

        public void displayLearningPanel()
        {
            //set all panels invisible
            HomePanel.Visible = false;
            ProductivityPanel.Visible = false;
            ExercisePanel.Visible = false;

            //hide necessary controls if trees are up-to-date
            if (model.learningUpToDate())
            {
                LearningControl.InfoBox.Enabled = false;
                LearningControl.SubmitButton.Enabled = false;
            }

            else
            {
                LearningControl.InfoBox.Enabled = true;
                LearningControl.SubmitButton.Enabled = true;
            }

            //set date information
            LearningControl.DayLabel.Text = model.LearningDate.ToString("dddd");
            LearningControl.DateLabel.Text = model.LearningDate.ToString("d");

            //display only the productivity panel
            LearningPanel.Visible = true;
        }

        public void displayExercisePanel()
        {
            //set all panels invisible
            HomePanel.Visible = false;
            ProductivityPanel.Visible = false;
            LearningPanel.Visible = false;

            //hide necessary controls if trees are up-to-date
            if (model.exerciseUpToDate())
            {
                ExerciseControl.InfoBox.Enabled = false;
                ExerciseControl.SubmitButton.Enabled = false;
            }

            else
            {
                ExerciseControl.InfoBox.Enabled = true;
                ExerciseControl.SubmitButton.Enabled = true;
            }

            //set date information
            ExerciseControl.DayLabel.Text = model.ExerciseDate.ToString("dddd");
            ExerciseControl.DateLabel.Text = model.ExerciseDate.ToString("d");

            //display only the productivity panel
            ExercisePanel.Visible = true;
        }

        private void setUpdateNotifications()
        {
            //determine if productivity entries aren't up to date
            if (!model.productivityUpToDate())
            {
                HomeProductivityUpdate.Visible = true;
            }

            else
            {
                //clear notification
                HomeProductivityUpdate.Visible = false;
            }

            //determine if learning entries aren't up to date
            if (!model.learningUpToDate())
            {
                HomeLearningUpdate.Visible = true;
            }

            else
            {
                //clear notification
                HomeLearningUpdate.Visible = false;
            }

            //determine if exercise entries aren't up to date
            if (!model.exerciseUpToDate())
            {
                HomeExerciseUpdate.Visible = true;
            }

            else
            {
                //clear notification
                HomeExerciseUpdate.Visible = false;
            }
        }

        public void updateDate()
        {
            //display date in dd/mm/yyyy format
            HomeDate.Text = model.CurrentDate.ToString("d");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void HomeDateTimer_Tick(object sender, EventArgs e)
        {
            if (HomeDate.Font.Size <= HomeDateDefaultFontSize)
            {
                HomeDateTimer.Stop();
                HomeDate.Font = Orchard.Controller.OrchardController.ChangeFontSize(HomeDate.Font, HomeDateDefaultFontSize);
            }
            else
            {
                HomeDate.Font = Orchard.Controller.OrchardController.ChangeFontSize(HomeDate.Font, HomeDate.Font.Size*.95f);
            }
        }
    }
}
